﻿using System.Collections.Generic;
using System.Linq;
using Harvest.Api.Model;
using Harvest.Api.Model.Internal;
using Harvest.Api.Types;

namespace Harvest.Api.ExtensionMethods
{

    public static class ProjectExtensions
    {

        public static Project ToProject(this ProjectItem entity)
        {
            var result = new Project
            {
                Active = entity.Active,
                Billable = entity.Billable,
                BillBy = entity.BillBy,
                Budget = entity.Budget.HasValue ? entity.Budget.Value : (double?)null,
                BudgetBy = entity.BudgetBy,
                ClientId = entity.ClientId.HasValue ? entity.ClientId.Value : (int?)null,
                Code = entity.Code,
                Fees = entity.Fees,
                Id = entity.Id.HasValue ? entity.Id.Value : (int?)null,
                Name = entity.Name,
                Notes = entity.Notes,
                Client = entity.Client
            };
            return result;
        }


        public static ProjectItem ToProjectItem(this Project entity)
        {
            var id = new NullableInt();
            if (entity.Id.HasValue) id = entity.Id.Value;

            var clientId = new NullableInt();
            if (entity.ClientId.HasValue) clientId = entity.ClientId.Value;

            var budget = new NullableDouble();
            if (entity.Budget.HasValue) budget = entity.Budget.Value;

            var result = new ProjectItem
            {
                Active = entity.Active,
                Billable = entity.Billable,
                BillBy = entity.BillBy,
                Budget = budget,
                BudgetBy = entity.BudgetBy,
                ClientId = clientId,
                Code = entity.Code,
                Fees = entity.Fees,
                Id = id,
                Name = entity.Name,
                Notes = entity.Notes,
                Client = entity.Client
            };
            return result;
        }


        public static List<Project> ToProjects(this IEnumerable<ProjectItem> entity)
        {
            var result = entity.Select(x => x.ToProject()).ToList();
            return result;
        }


        public static List<ProjectItem> ToProjectsItem(this IEnumerable<Project> entity)
        {
            var result = entity.Select(x => x.ToProjectItem()).ToList();
            return result;
        }
    }

}
